package com.thor.palmmall.card.common;

import java.io.IOException;

import org.apache.commons.codec.binary.Base64;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

/**
 * @author liyang
 * @since 0.1
 */
public class BasicAuthorizationInterceptor implements ClientHttpRequestInterceptor {

  private String userName;
  private String password;

  @Override
  public ClientHttpResponse intercept(HttpRequest request, byte[] body,
      ClientHttpRequestExecution execution) throws IOException {
    String token = Base64.encodeBase64String((userName + ":" + password).getBytes("utf-8"));

    request.getHeaders().add("Authorization", "Basic " + token);
    return execution.execute(request, body);
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

}
